package edu.netjava.Queue;

/**
 *  This class is created  to act like ArrayList
 *  @author tan
 */

public class DaArray {
	
    Object[] objects ;
	
	int size = 10;  //initial the length of the Array
	int index=-1;   
	float pre = .2f;
	
	/**
	 * the constructor to create Object array
	 */
	public DaArray(){
		objects = new Object[size];
	}
	
	/**
	 * 
	 * @param element: the element to add into the array
	 * @return objects: the fixed array
	 */
	public void addElement(Object element){
		index++;
		Object[] temp = new Object[size];
		if(index >= size){
			size += size*pre;
			temp = new Object[size];
			System.arraycopy(objects, 0, temp, 0, objects.length);
		}
		temp[index]=element;
		objects = temp;
		temp = null;
	}
	
	/**
	 * 
	 * @param index:the location of the element in the array
	 */
	public Object deleteElement(int index){
		
		if(index<0 || index>size){
			System.out.println("the range of index is illegel ");
		}
		int deleNum = size - index -1;
		Object obj = objects[index];
		System.arraycopy(objects, index+1, objects, index, deleNum);
	    return obj;
	}
	
	/**
	 * 
	 * @param index: the location of the wanted element
	 * @return object :the wanted element in the array
	 */
	
	public Object getElement(int index){
		return objects[index];
	}
	
	/**
	 * print all the elements in the array
	 */
	public void printAllElement(){
		for(int i=0;i<objects.length;i++){
			output(objects[i]);
		}
	}
	
	/**
	 * 
	 * @param obj :the object needed to print
	 */
	public static void output(Object obj){
		System.out.println(obj);
	}
	
	public static void main(String args[]){
		DaArray da = new DaArray();
	
	}

}
